import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import '../utils/kudo_colors.dart';
import '../router/app_navigation_manager.dart';

typedef LoginRequiredCallback = void Function();

class LoginRequiredDialog extends StatefulWidget {
  final String title;
  final String message;
  final LoginRequiredCallback? onLogin;

  const LoginRequiredDialog({
    super.key,
    this.title = 'Login Required',
    this.message = 'Please login to access this feature',
    this.onLogin,
  });

  static Future<void> show({
    required BuildContext context,
    String title = 'Login Required',
    String message = 'Please login to access this feature',
    LoginRequiredCallback? onLogin,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder:
          (context) => LoginRequiredDialog(
            title: title,
            message: message,
            onLogin: onLogin,
          ),
    );
  }

  @override
  State<LoginRequiredDialog> createState() => _LoginRequiredDialogState();
}

class _LoginRequiredDialogState extends State<LoginRequiredDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;
  late Animation<double> _fadeAnimation;

  @override
  void initState() {
    super.initState();
    _initializeAnimations();
    _animationController.forward();
  }

  void _initializeAnimations() {
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );

    _scaleAnimation = Tween<double>(begin: 0.8, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeOutBack),
    );

    _fadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(parent: _animationController, curve: Curves.easeOut),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animationController,
      builder: (context, child) {
        return FadeTransition(
          opacity: _fadeAnimation,
          child: ScaleTransition(
            scale: _scaleAnimation,
            child: Dialog(
              backgroundColor: Colors.transparent,
              child: _buildDialogContent(),
            ),
          ),
        );
      },
    );
  }

  Widget _buildDialogContent() {
    return Container(
      constraints: BoxConstraints(
        maxWidth: 350.w,
        maxHeight: MediaQuery.of(context).size.height * 0.6,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.1),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [_buildHeader(), _buildContent(), _buildActionButtons()],
      ),
    );
  }

  Widget _buildHeader() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(20.w),
      decoration: BoxDecoration(
        color: KudoColors.green.withValues(alpha: 0.1),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20.r),
          topRight: Radius.circular(20.r),
        ),
      ),
      child: Column(
        children: [
          Container(
            width: 40.w,
            height: 40.h,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/logo.png'),
              ),
            ),
          ),
          SizedBox(height: 8.h),
          Text(
            widget.title,
            style: GoogleFonts.lato(
              fontSize: 18.sp,
              fontWeight: FontWeight.bold,
              color: KudoColors.black,
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _buildContent() {
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            widget.message,
            style: GoogleFonts.lato(
              fontSize: 14.sp,
              color: Colors.grey.shade700,
              height: 1.4,
            ),
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _buildActionButtons() {
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Row(
        children: [
          Expanded(child: _buildCancelButton()),
          SizedBox(width: 12.w),
          Expanded(child: _buildLoginButton()),
        ],
      ),
    );
  }

  Widget _buildCancelButton() {
    return Container(
      height: 44.h,
      decoration: BoxDecoration(
        color: Colors.grey.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(12.r),
        border: Border.all(color: Colors.grey.withValues(alpha: 0.3)),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(12.r),
          onTap: () => Navigator.of(context).pop(),
          child: Center(
            child: Text(
              'Cancel',
              style: GoogleFonts.lato(
                fontSize: 14.sp,
                fontWeight: FontWeight.w600,
                color: Colors.grey,
              ),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildLoginButton() {
    return Container(
      height: 44.h,
      decoration: BoxDecoration(
        color: KudoColors.green,
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(12.r),
          onTap: _handleLogin,
          child: Center(
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(Icons.login, size: 16.sp, color: Colors.white),
                SizedBox(width: 6.w),
                Text(
                  'Login',
                  style: GoogleFonts.lato(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _handleLogin() {
    Navigator.of(context).pop(); // 先关闭弹窗
    if (widget.onLogin != null) {
      widget.onLogin!();
    } else {
      // 导航到登录页面
      AppNavigationManager.navigateToCredentialEntry(context);
    }
  }
}
